package com.tanhua.server.interceptor;

import com.tanhua.utils.Cache;
import lombok.SneakyThrows;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.MethodParameter;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.MediaType;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.http.server.ServletServerHttpRequest;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@ControllerAdvice
public class MyResponseBodyAdvice implements ResponseBodyAdvice {

    @Value("${tanhua.cache.enable}")
    private Boolean cacheEnable;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    //首先需要判断需要增加响应通知的方法都具有哪些特点, 对方法进行筛选, 该方法返回true才会执行下面的beforeBodyWrite方法
    @Override
    public boolean supports(MethodParameter returnType, Class converterType) {
//        AnnotatedElement annotatedElement = returnType.getAnnotatedElement();
//        Annotation[] annotations = annotatedElement.getAnnotations();
//        for (Annotation annotation : annotations) {
//            if (cacheEnable && annotation instanceof Cache){
//                //是get请求且开启了缓存支持
//                return true;
//            }
//        }
//        return false;
        return cacheEnable && returnType.hasMethodAnnotation(Cache.class) && returnType.hasMethodAnnotation(GetMapping.class);
    }

    @SneakyThrows
    @Override
    public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
        //如果方法的返回值是null的话, 就不需要写入缓存了
        if (null == body){
            return null;
        }


        //返回值不是null, 则将方法的返回值写入缓存
        HttpServletRequest servletRequest = ((ServletServerHttpRequest) request).getServletRequest();
        String requestURI = servletRequest.getRequestURI();
        Map<String, String[]> parameterMap = servletRequest.getParameterMap();
        String requestParam = RedisCacheInterceptor.objectMapper.writeValueAsString(parameterMap);
        String token = servletRequest.getHeader("Authorization");

        String key = DigestUtils.md5Hex(requestURI + "_" + requestParam + "_" + token);

        //获取注解上的超时时间
        Cache methodAnnotation = returnType.getMethodAnnotation(Cache.class);
        String time = methodAnnotation.time();

        if (body instanceof String){
            redisTemplate.opsForValue().set(key,(String)body, Long.valueOf(time));
        }
        redisTemplate.opsForValue().set(key,RedisCacheInterceptor.objectMapper.writeValueAsString(body),Long.valueOf(time));

        return null;
    }
}
